package com.aruba.rtmppushapplication.push.natives;

import android.util.Log;

/**
 * Created by aruba on 2021/1/30.
 */
public class NativePush {
    private static final String TAG = NativePush.class.getSimpleName();

    static {
        System.loadLibrary("native-lib");
    }

    private LiveStateChangeListener mListener;

    public void setLiveStateChangeListener(LiveStateChangeListener listener) {
        mListener = listener;
    }

    /**
     * native层回调
     *
     * @param code -96:音频编码器设置失败
     *             -97:音频编码器打开失败
     *             -98:打开视频编码器失败
     *             -99:建立rtmp连接失败
     *             -100:rtmp断开
     */
    public void onPostNativeError(int code) {
        Log.e(TAG, "onPostNativeError:" + code);
        //停止推流
        stopPush();
        Log.d("NativePush", code + "");
        if (null != mListener) {
            mListener.onErrorPusher(code);
        }
    }

    /**
     * native层回调
     * 推流连接建立和线程退出
     *
     * @param state
     */
    public void onPostNativeState(int state) {
        if (state == 100) {
            mListener.onStartPusher();
        } else if (state == 101) {
            mListener.onStopPusher();
        }
    }

    //设置视频参数
    public native void setVideoParams(int width, int height, int bitrate, int fps);

    //设置音频参数
    public native void setAudioParams(int sample, int channel);

    //推视频帧
    public native void pushVideo(byte[] buffer);

    //推音频帧
    public native void pushAudio(byte[] buffer, int size);

    //开始推流线程
    public native void startPush(String url);

    //停止推流
    public native void stopPush();

    //获取音频缓冲区大小
    public native int getInputSamples();
}